package com.world.rickcloudendpoint.common.utils;

import com.world.rickcloudendpoint.common.constant.GlobalConstants;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 公共参数校验
 *
 * @author Ja
 * @since 2019/07/22
 **/
public class CommonCheckUtils {

    /**
     * 校验ipv4
     *
     * @param addr ip地址
     * @return true:ipv4格式正确，false：ipv4格式不正确
     */
    public static boolean ipv4Check(String addr) {
        if (null == addr) {
            return false;
        }

        Pattern pattern = Pattern.compile(GlobalConstants.Constants.IPV4_REG_EXP);
        Matcher matcher = pattern.matcher(addr);

        return matcher.find();
    }

    /**
     * 校验ipv6
     *
     * @param addr ip地址
     * @return true:ipv6格式正确，false：ipv6格式不正确
     */
    public static boolean ipv6Check(String addr) {
        if (null == addr) {
            return false;
        }

        Pattern pattern = Pattern.compile(GlobalConstants.Constants.IPV6_REG_EXP);
        Matcher matcher = pattern.matcher(addr);

        return matcher.find();
    }

    /**
     * 端口校验，0~65535
     *
     * @param port 端口字符串
     * @return 端口在0~65535之间
     */
    public static boolean portCheck(String port) {
        int portTemp;
        try {
            portTemp = Integer.parseInt(port);
        } catch (NumberFormatException e) {
            return false;
        }

        return portTemp >= 0 && portTemp <= 65535;
    }

    /**
     * 校验协议数值范围 0~255
     *
     * @param protocol 协议数值
     * @return 协议数值合法
     */
    public static boolean protocolCheck(String protocol) {
        int proTemp;
        try {
            proTemp = Integer.parseInt(protocol);
        } catch (NumberFormatException e) {
            return false;
        }

        return proTemp >= 0 && proTemp <= 255;
    }

    /**
     * 域名格式校验
     *
     * @param host 域名
     * @return true:域名格式正确，false:域名格式错误
     */
    public static boolean domainCheck(String host) {
        if (null == host) {
            return false;
        }

        Pattern pattern = Pattern.compile(GlobalConstants.Constants.HOST_REG_EXP);
        Matcher matcher = pattern.matcher(host);

        return matcher.find();
    }


    /**
     * 偏移量效验，0~64
     *
     * @param offset 偏移量
     * @return 偏移量在0~64之间
     */
    public static boolean offsetCheck(String offset) {
        int offsetTemp;
        try {
            offsetTemp = Integer.parseInt(offset);
        } catch (NumberFormatException e) {
            return false;
        }

        if(offsetTemp >= 0 && offsetTemp <= 64){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 字符串转化成十六进制
     *
     * @param keyword 特征码
     * @return 特征码的十六进制字符串
     */
    public static String str2HexStr(String keyword) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = keyword.getBytes();
        int bit;
        for(int i=0; i<bs.length; i++){
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
        }
        return sb.toString().trim();
    }
}

